﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Reminder
{
    public partial class Form1 : Form
    {

        private System.Windows.Forms.Timer m_SheduleTimer = null;
        private System.Windows.Forms.Timer m_StartTimer = null;

        private bool notShow = false;

        NotifyIcon notif;

        public Form1()
        {
            InitializeComponent();

            notif = new NotifyIcon();
            notif.Icon = Properties.Resources.ico;
            notif.Visible = true;
            notif.Click += item_Show;

            this.ContextMenu = new ContextMenu();
            this.ContextMenu.MenuItems.Add("Дефолтний фон", set_DefBack);
            this.ContextMenu.MenuItems.Add("Встановити фон", set_Back);
            this.ContextMenu.MenuItems.Add("Встановити час", set_Period);
            //notif.ContextMenu.MenuItems.Add("Показати", item_Show);
            this.ContextMenu.MenuItems.Add("Закрити", item_Click);


            init();

            ShowInTaskbar = false;
            Hide();
            WindowState = System.Windows.Forms.FormWindowState.Minimized;
            
            _initTimers();
        }

        private void _initTimers()
        {

            m_StartTimer = new System.Windows.Forms.Timer();
            m_StartTimer.Tick += new EventHandler(Scheduler_Start);

            var ts = TimeSpan.FromMinutes(Reminder.Properties.Settings.Default.Interval - DateTime.Now.Minute);

            m_StartTimer.Interval = (int)ts.TotalMilliseconds;
            m_StartTimer.Start();

            m_SheduleTimer = new System.Windows.Forms.Timer();
            m_SheduleTimer.Tick += new EventHandler(Scheduler);
            m_SheduleTimer.Interval = (int)TimeSpan.FromMinutes(Reminder.Properties.Settings.Default.Interval).TotalMilliseconds;            
        }

        public void init()
        {
            try
            {
                Height = Screen.PrimaryScreen.WorkingArea.Height;
                var orig = Properties.Resources.backgr;
                if (!String.IsNullOrEmpty(Reminder.Properties.Settings.Default.BackImg))
                    orig = new Bitmap(Reminder.Properties.Settings.Default.BackImg);
                int newW = (int)(orig.Width * (double)((double)Screen.PrimaryScreen.WorkingArea.Height / (double)orig.Height));
                Width = newW;
                BackgroundImage = ResizeBitmap(orig, newW, Screen.PrimaryScreen.WorkingArea.Height);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
        {
            Bitmap result = new Bitmap(nWidth, nHeight);
            using (Graphics g = Graphics.FromImage((Image)result))
                g.DrawImage(b, 0, 0, nWidth, nHeight);
            return result;
        }

        void item_Click(object sender, EventArgs e)
        {
            Close();
        }

        void item_Show(object sender, EventArgs e)
        {
            _show();
        }

        private void _show()
        {
            if (WindowState != FormWindowState.Minimized)
            {
                return;
            }
            if (!ShowInTaskbar)
                ShowInTaskbar = true;

            WindowState = FormWindowState.Normal;
            Show();
            
            button1.Left = 100;
            button1.Refresh();
            button2.Left = (int)(Math.Ceiling((double)Width / 2) - (int)Math.Ceiling((double)button2.Width / 2));
            button2.Refresh();
            button3.Left = (int)(Width - 200);
            button3.Refresh();

            Application.DoEvents();
        }

        void set_Back(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Reminder.Properties.Settings.Default.BackImg = openFileDialog1.FileName;
                Reminder.Properties.Settings.Default.Save();
                init();
                _show();
            }
        }

        void set_Period(object sender, EventArgs e)
        {
            var f = new Form2(Reminder.Properties.Settings.Default.Interval);
            f.ShowDialog();

            Reminder.Properties.Settings.Default.Interval = f.Interval;
            Reminder.Properties.Settings.Default.Save();

            _initTimers();
            _show();
        }

        void set_DefBack(object sender, EventArgs e)
        {
            Reminder.Properties.Settings.Default.BackImg = "";
            Reminder.Properties.Settings.Default.Save();
            init();
            _show();
        }

        private void Scheduler_Start(Object myObject, EventArgs myEventArgs)
        {
            Scheduler(myObject, myEventArgs);
            m_SheduleTimer.Start();
            m_StartTimer.Stop();
        }

        private void Scheduler(Object myObject, EventArgs myEventArgs)
        {
            if (WindowState == FormWindowState.Minimized && !notShow)
            {
                /*switch (DateTime.Now.Hour)
                {
                    case 12:
                        label1.Text = "Обід";
                        break;
                    case 18:
                        label1.Text = "Додому йди";
                        break;
                    default:
                        label1.Text = "Перерва";
                        break;
                }*/
                switch (DateTime.Now.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        label1.Text = "День рефакторингу";
                        break;
                    case DayOfWeek.Tuesday:
                        label1.Text = "День продуктивності";
                        break;
                    case DayOfWeek.Wednesday:
                        label1.Text = "День інформаційної безпеки";
                        break;
                    case DayOfWeek.Thursday:
                        label1.Text = "День документації";
                        break;
                    default:
                        return;
                }

                if (!ShowInTaskbar)
                    ShowInTaskbar = true;

                WindowState = FormWindowState.Normal;
                Show();
                Application.DoEvents();
            }
        }

        private void Form1_Click(object sender, EventArgs e)
        {
            Hide();
            WindowState = FormWindowState.Minimized;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            // sendEndMail();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            notShow = true;
            Hide();
            WindowState = FormWindowState.Minimized;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            notShow = true;
            Hide();
            WindowState = FormWindowState.Minimized;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Hide();
            WindowState = FormWindowState.Minimized;
        }
    }
}
